package com.testplatfrom.project.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.testplatfrom.common.core.exception.project.ProjectException;
import com.testplatfrom.common.security.utils.SecurityUtils;
import com.testplatfrom.project.domain.AssertionPositionEnum;
import com.testplatfrom.project.domain.TestplatfromAssert;
import com.testplatfrom.project.domain.TestplatfromDatabase;
import com.testplatfrom.project.enumproject.environment.AssertTypeEnum;
import com.testplatfrom.project.enumproject.environment.JsonPathEnum;
import com.testplatfrom.project.mapper.TestplatfromAssertMapper;
import com.testplatfrom.project.service.TestplatfromAssertService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2023-08-28
 */
@Service
public class TestplatfromAssertServiceImpl extends ServiceImpl<TestplatfromAssertMapper, TestplatfromAssert> implements TestplatfromAssertService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addOrUpdateTestplatfromAssert(List<TestplatfromAssert> testplatfromAssertList,String environmentId,Boolean bool) {
        for (TestplatfromAssert testplatfromAssert : testplatfromAssertList) {
            //判断断言类型是否存在
            String assertAssertType = testplatfromAssert.getAssertType();
            AssertTypeEnum assertTypeEnum = AssertTypeEnum.valueOf(assertAssertType);
            if (ObjectUtil.isNull(assertTypeEnum)){
                throw new ProjectException("断言类型不能为空");
            }
            //如果说正则表达式 则判断正则表达式以及对象是否正常
            if (assertTypeEnum.name().equals("REGULAR")){
                //获取断言位置
                AssertionPositionEnum assertionPositionEnum = AssertionPositionEnum.getByValue(testplatfromAssert.getAssertionPosition());
                if (ObjectUtil.isNull(assertionPositionEnum)){
                    throw new ProjectException("断言位置类型错误");
                }
                if (ObjectUtil.isEmpty(testplatfromAssert.getRegularValue())){
                    throw new ProjectException("正则值不能为空");
                }
                testplatfromAssert.setAssertionPosition(assertionPositionEnum.name());
            }
            if (assertTypeEnum.name().equals("JSONPATH")){
                JsonPathEnum jsonPathEnum = JsonPathEnum.getByKey(testplatfromAssert.getJsonpathType());
                if (ObjectUtil.isNull(jsonPathEnum)){
                    throw new ProjectException("JSONPath方法错误");
                }
                if (ObjectUtil.isEmpty(testplatfromAssert.getJsonpath())||ObjectUtil.isEmpty(testplatfromAssert.getJsonpathValue())){
                    throw new ProjectException("JSONPath或者JSONPath值不能为空");
                }
                testplatfromAssert.setJsonpathType(jsonPathEnum.name());
            }
            if (assertTypeEnum.name().equals("XPATH")){
                if (ObjectUtil.isEmpty(testplatfromAssert.getXpathValue())){
                    throw new ProjectException("XPATH值不能为空");
                }
            }
            if (assertTypeEnum.name().equals("REPONSETIME")){
                if (ObjectUtil.isNull(testplatfromAssert.getResponseTime())){
                    throw new ProjectException("响应时间不难为空");
                }
            }
            testplatfromAssert.setCreateBy(SecurityUtils.getUsername());
            testplatfromAssert.setCreateTime(LocalDateTime.now());
        }
        if (!bool){
            LambdaQueryWrapper<TestplatfromAssert> testplatfromHttpconfigurationLambdaQueryWrapper=new LambdaQueryWrapper<>();
            testplatfromHttpconfigurationLambdaQueryWrapper.eq(TestplatfromAssert::getEnvironmentId, environmentId);
            this.remove(testplatfromHttpconfigurationLambdaQueryWrapper);
        }
        return saveBatch(testplatfromAssertList);
    }

    @Override
    public List<TestplatfromAssert> getTestplatfromAssert(String environmentId) {
        List<TestplatfromAssert> testplatfromAsserts = this.list(new LambdaQueryWrapper<TestplatfromAssert>().eq(TestplatfromAssert::getEnvironmentId, environmentId));
        if (CollectionUtils.isEmpty(testplatfromAsserts)){
            return null;
        }
        for (TestplatfromAssert testplatfromAssert : testplatfromAsserts) {
            //如果说正则表达式 则判断正则表达式以及对象是否正常
            if (testplatfromAssert.getAssertType().equals("REGULAR")){
                //获取断言位置
                AssertionPositionEnum assertionPositionEnum = AssertionPositionEnum.valueOf(testplatfromAssert.getAssertionPosition());
                testplatfromAssert.setAssertionPosition(assertionPositionEnum.name());
            }
            if (testplatfromAssert.getAssertType().equals("JSONPATH")){
                JsonPathEnum jsonPathEnum = JsonPathEnum.valueOf(testplatfromAssert.getJsonpathType());
                testplatfromAssert.setJsonpathType(jsonPathEnum.name());
            }
        }
        return testplatfromAsserts;
    }
}
